package com.yonyou.pmclouds.billview.plugin;

import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.BeanHelper;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.basecom.util.StorageFactory;
import com.yonyou.pmclouds.businessobject.util.BusinessObjectConsts;
import com.yonyou.pmclouds.businessobject.util.PrintPreviewObj2Html;
import com.yonyou.pmclouds.enterprise.entity.EnterpriseInfo;
import com.yonyou.pmclouds.outter.enterprise.EnterpriseInfoQuery;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.workstartreport.entity.WorkStartCheckVO;
import com.yonyou.pmclouds.workstartreport.entity.WorkStartVO;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Component
public class WorkStartPlugin implements IBillViewCustomPlugin {

    @Autowired
    private EnterpriseInfoQuery enterpriseInfoQuery;
    @Autowired
    private UserInfoQuery userInfoQuery;
    @Autowired
    private StorageFactory storageFactory;
    @Value("${print.perLineNum}")
    private int perLineNum;
    @Override
    public Object[] fillListBuziField(Object[] objects) throws BusinessException {
        //施工单位
        Set<String> constructionUnitPks = new HashSet<>();
        WorkStartVO[] workStartVOSS=new WorkStartVO[objects.length];
        for (int i = 0; i < objects.length ; i++){
            workStartVOSS[i]=(WorkStartVO)objects[i];
        }

        // 取到所有施工单位的pk,获取施工单位信息
        for (WorkStartVO workStartVO : workStartVOSS) {
            constructionUnitPks.add(workStartVO.getPkEnterprise());
        }
        Map<String, EnterpriseInfo> enterpriseInfoMap = enterpriseInfoQuery
                .queryEnterpriseByIds(constructionUnitPks.toArray(new String[0]));
        for (WorkStartVO workStartVO : workStartVOSS) {
            EnterpriseInfo enterpriseInfo = enterpriseInfoMap.get(workStartVO.getPkEnterprise());
            workStartVO.setEnterpriseName(enterpriseInfo==null?"":enterpriseInfo.getName());
        }

        //监理单位
        enterpriseInfoQuery.tranEntInfoField(workStartVOSS, new String[]{WorkStartVO.PK_SUPERVISION}, new String[]{WorkStartVO.SUPERVISION_NAME});
        return workStartVOSS;
    }

    @Override
    public Map<String,Object[]> fillDetailBuziField(SuperVO superVO, HttpServletRequest request) throws BusinessException {

        //获取返回的单据检查项
        WorkStartCheckVO[] workStartCheckVOS=(WorkStartCheckVO[]) BeanHelper.getProperty(superVO, "workStartCheckVOS");

        //补充创建者姓名
        userInfoQuery.tranUserInfoField(new Object[]{superVO},new String[]{CommonFieldConst.CREATOR},new String[]{CommonFieldConst.CREATOR_NAME});
        //获取施工方ID
        String pkEnterprise = BeanHelper.getProperty(superVO,"pkEnterprise")==null?"":(String)BeanHelper.getProperty(superVO,"pkEnterprise");
        String pkSupervision = ((WorkStartVO)superVO).getPkSupervision();


        Map<String, EnterpriseInfo> enterpriseInfoMap = enterpriseInfoQuery
                .queryEnterpriseByIds(new String[]{pkEnterprise,pkSupervision});
        if(MapUtils.isNotEmpty(enterpriseInfoMap)){
            EnterpriseInfo enterpriseInfo = enterpriseInfoMap.get(pkEnterprise);
            EnterpriseInfo supInfo = enterpriseInfoMap.get(pkSupervision);
            //补充施工方名称
            BeanHelper.setProperty(superVO,"enterpriseName",enterpriseInfo==null?"":enterpriseInfo.getName());
            ((WorkStartVO)superVO).setSupervisionName(supInfo == null ?"":supInfo.getName());
        }

        PrintPreviewObj2Html obj2Html = new PrintPreviewObj2Html(PrintPreviewObj2Html.BASE_PREFIX, PrintPreviewObj2Html.IMG, perLineNum);

        if (workStartCheckVOS != null) {
            for (WorkStartCheckVO workStartCheckVO : workStartCheckVOS) {
                if (workStartCheckVO.getPictures() != null) {
                    //转换图片解析为HTML
                    String picHtml = obj2Html.urls2Html(storageFactory.getStorageUtil().signUrlForMultiPictures(workStartCheckVO.getPictures(), null, request));
                    workStartCheckVO.setPicture_address(picHtml);
                }
            }
            BeanHelper.setProperty(superVO,"workStartCheckVOS",workStartCheckVOS);
        }

        Map<String,Object[]> resultMap = new HashMap<String,Object[]>();

        resultMap.put(BusinessObjectConsts.PM_WORKSTART, new Object[]{superVO});
        resultMap.put(BusinessObjectConsts.PM_WORKSTARTCHECK, workStartCheckVOS);

        return resultMap;
    }

    @Override
    public String getBillType() {
        return BillTypeConst.WORKSTART_BILLTYPE;
    }
}
